package com.vidmt.api.modules.sys.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageInfo;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.vidmt.api.common.web.WebController;
import com.vidmt.api.modules.sys.entity.Property;
import com.vidmt.api.modules.sys.service.PropertyService;

/**
 * 系统属性Controller
 * 
 * @author xingqisheng
 * @version 2015-08-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/property")
public class WebPropertyController extends WebController {

	@Autowired
	private PropertyService PropertyService;

	@ModelAttribute
	public Property get(@RequestParam(required = false) String id) {
		Property entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = PropertyService.get(id);
		}
		if (entity == null) {
			entity = new Property();
		}
		return entity;
	}

	@RequiresPermissions("sys:Property:view")
	@RequestMapping(value = { "list", "" })
	public String list(Property Property, Integer pageNum, Integer pageSize, Model model) {
		PageInfo<Property> page = PropertyService.findPage(pageNum, pageSize, Property);
		// com.thinkgem.jeesite.common.persistence.Page<Property> mypage = new
		// com.thinkgem.jeesite.common.persistence.Page<>(page.getPageNum(),page.getPageSize());
		// mypage.setList(page);
		model.addAttribute("page", page);
		return "modules/sys/propertyList";
	}

	@RequiresPermissions("sys:Property:view")
	@RequestMapping(value = "form")
	public String form(Property Property, Model model) {
		model.addAttribute("Property", Property);
		return "modules/sys/propertyForm";
	}

	@RequiresPermissions("sys:Property:edit")
	@RequestMapping(value = "save")
	public String save(Property Property, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, Property)) {
			return form(Property, model);
		}
		PropertyService.saveOrUpdate(Property);
		addMessage(redirectAttributes, "保存系统属性成功");
		return "redirect:" + Global.getAdminPath() + "/sys/property/?repage";
	}

	@RequiresPermissions("sys:Property:edit")
	@RequestMapping(value = "delete")
	public String delete(Property Property, RedirectAttributes redirectAttributes) {
		PropertyService.delete(Property);
		addMessage(redirectAttributes, "删除系统属性成功");
		return "redirect:" + Global.getAdminPath() + "/sys/property/?repage";
	}
}